﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace CoreModule
{
    public class DbColumnManager
    {
        /// <summary>
        /// 当前的 (100起步）
        /// </summary>
        public int CurrentSortIndex { get; set; } = 100;

        public static string XmlFilePath = "DbColumnConfigurationInfos.xml";

        public List<DbColumnConfigurationInfo> DbColumnConfigurationInfos = new List<DbColumnConfigurationInfo>();
       
        /// <summary>
        /// 
        /// </summary>
        public DbColumnManager() {

        }
        /// <summary>
        /// 使用的列表
        /// </summary>
        /// <returns></returns>
        public List<DbColumnConfigurationInfo> GetUsedList()
        {
            return DbColumnConfigurationInfos
                .Where(item=>item.DbColumnStatus== DbColumnStatus.Used)
                .OrderBy(item=>item.SortIndex)
                .ToList();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public List<DbColumnConfigurationInfo> GetCheckedList()
        {
            return DbColumnConfigurationInfos
                .Where(item => item.DbColumnStatus == DbColumnStatus.Used && item.IsChecked)
                .OrderBy(item => item.SortIndex)
                .ToList();
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="title"></param>
        /// <returns></returns>
        public void Delete(string title)
        {
          var first=  DbColumnConfigurationInfos.FirstOrDefault(item => item.FieldName == title);
           if (first!=null)
            {
                first.DbColumnStatus = DbColumnStatus.NotUsed;
                first.IsChecked = false;
                first.SortIndex = -1;
            }
        }
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="title"></param>
        /// <returns></returns>
        public DbColumnConfigurationInfo Create(string title)
        {
            CurrentSortIndex=CurrentSortIndex+1;

            var  newCln = DbColumnConfigurationInfos
                .FirstOrDefault(item => item.DbColumnStatus == DbColumnStatus.NotUsed);
            if (newCln!=null)
            {
                newCln.FieldName = title;
                newCln.SortIndex = CurrentSortIndex;
                newCln.DbColumnStatus = DbColumnStatus.Used;
                newCln.IsChecked = true;
                return newCln;
            }
          
            return null;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static DbColumnManager Load()
        {
            if (File.Exists(XmlFilePath))
            {
                try
                {
                    using (FileStream fileStream = new FileStream(XmlFilePath, FileMode.OpenOrCreate))
                    {
                        var confNew = (DbColumnManager)new XmlSerializer(typeof(DbColumnManager)).Deserialize(fileStream);

                        if (confNew.DbColumnConfigurationInfos.Count == 0)
                        {
                            for (int i = 0; i < 20; i++)
                            {
                                confNew.DbColumnConfigurationInfos.Add(new DbColumnConfigurationInfo()
                                {
                                    ColumnName = string.Format("Cln{0}", i)
                                });
                            }
                        }
                        return confNew;
                    }
                }
                catch (Exception)
                {
                    File.Delete(XmlFilePath);
                    var confDefault = new DbColumnManager();

                    if (confDefault.DbColumnConfigurationInfos.Count == 0)
                    {
                        for (int i = 0; i < 20; i++)
                        {
                            confDefault.DbColumnConfigurationInfos.Add(new DbColumnConfigurationInfo()
                            {
                                ColumnName = string.Format("Cln{0}", i)
                            });
                        }
                    }
                    return confDefault;
                }
            }
            ///
            var conf = new DbColumnManager();

            if (conf.DbColumnConfigurationInfos.Count == 0)
            {
                for (int i = 0; i < 20; i++)
                {
                    conf.DbColumnConfigurationInfos.Add(new DbColumnConfigurationInfo()
                    {
                        ColumnName = string.Format("Cln{0}", i)
                    });
                }
            }
            return conf;

        }

        /// <summary>
        /// /
        /// </summary>
        public void Save()
        {
            if (File.Exists(XmlFilePath))
            {
                File.Delete(XmlFilePath);
            }
            try
            {
                using (FileStream fileStream = new FileStream(XmlFilePath, FileMode.OpenOrCreate))
                {
                    new XmlSerializer(typeof(DbColumnManager)).Serialize(fileStream, this);
                }
            }
            catch (Exception)
            {
            }
        }
    }
}
